package com.lmh.demo.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.lmh.demo.dao.UserDao;
import com.lmh.demo.entity.User;

@RestController
public class UserController {
 
	@Autowired
	UserDao userdao;

    @Autowired
    private SolrClient solrClient;
	
	@ResponseBody
	@RequestMapping("/")
	public PageInfo<User> index() {
		//注意的地方PageHelper下面是查询数据方法
		PageHelper.startPage(1,2);
		List<User> UserList=userdao.findUserList();
	    PageInfo<User> appsPageInfo = new PageInfo<User>(UserList);
		return appsPageInfo;
	}
	
	@ResponseBody
	@RequestMapping("/main")
	public User main() {
		User user=userdao.findUser(1);
		return user;
	}
	@ResponseBody
	@RequestMapping("/solrDemo")
	public List solrdemo() throws SolrServerException, IOException {
		//根据id查询内容
		SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q", "*:*");
        solrQuery.set("start", 0);
        solrQuery.set("rows", 20);

        //======高亮设置===
        //开启高亮
        solrQuery.setHighlight(true);
        //高亮域
        solrQuery.addHighlightField("title");
        //前缀
        solrQuery.setHighlightSimplePre("<span style='color:red'>");
        //后缀
        solrQuery.setHighlightSimplePost("</span>");

        QueryResponse response = solrClient.query(solrQuery);
        SolrDocumentList results = response.getResults();

//        System.out.println("查询内容:" + solrQuery);
        Gson gson = new Gson();
        List VIPmap = new ArrayList();
        for(SolrDocument result:results) {
        	VIPmap.add(result.getFieldValue("role_name"));
        }
		return VIPmap;
	}
}
